package com.atguigu.bigdata.scala.mytest.chapter07

object Test_ScalaMatch {
  def main(args: Array[String]): Unit = {

    println("========匹配常量=========")
    def describe(x:Any)= x match {
      case "+" => "加法"
      case "-" => "减法"
      case "*" => "乘法"
      case _ => "什么玩意"
    }
    println(describe("+"))//加法
    println(describe("hello"))//什么玩意

    println("========匹配类型=========")
    // 此种场景下，case还起到了转换类型的概念，如果想要使用转换类型后的变量，那么需要声明变量
    //List泛型不起作用
    def describe1(x:Any)= x match {
      case a:Int => a+10
      case b:Boolean => !b
      case c:Array[Int] => "Array"+c
      case d:List[Int] =>d
      case someThing => "啥也不是"
    }
    println(describe1(12))//22
    println(describe1(false))//true
    println(describe1(Array(1,2,3)))//Array[I@19bb089b
    println(describe1(Array("1","2","3")))//啥也不是
    println(describe1(List(1,2,3)))//List(1, 2, 3)
    println(describe1(List("1","2","3")))//List(1, 2, 3)

    println("========匹配数组=========")
    for (arr <- Array(
      Array(0),
      Array(1, 0),
      Array(0, 1, 0),
      Array(1, 1, 0),
      Array(1, 1, 0, 1),
      Array(3, 1, 0, 1),
      Array("a", 90))) {
      val result = arr match {
        //匹配有两个元素的数组，然后将将元素值赋给对应的x,y
        case Array(x, y) => x + "," + y
        //匹配Array(0) 这个数组
        case Array(0) => "0"
        //匹配以0开头和数组
        case Array(0, _*) => "以0开头的数组"
        case Array(3,_*) => "以3开头的数组"
        case _ => "something else"
      }
      println("result = " + result)
    }

    println("========匹配列表=========")
    for (list <- Array(
      List(0),
      List(1, 0),
      List(0, 0, 0),
      List(1, 0, 0),
      List(1, 3, 0),
      List(88))) {
      val result1 = list match {
        case List(0) => "0" //匹配List(0)
        case List(x, y) => x + "," + y //匹配有两个元素的List
        case List(0, _*) => "0 ..."
        case List(1, 3,_*) => "1,3..."
        case _ => "something else"
      }
      println(result1)
    }

    //1::2::3::4::Nil 1-2-List(3, 4)
    val list: List[Int] = List(1,2,3,4)
    list match {
      //将集合分为三部分:第一个,第二个,剩余的
      case first :: second :: rest => println(first + "-" + second + "-" + rest)
      case _ => println("something else")
    }

    //1::2::Nil 1-2-List()
    val list1: List[Int] = List(1,2)
    list1 match {
      case first :: second :: rest => println(first + "-" + second + "-" + rest)
      case _ => println("something else")
    }

    //1::Nil something else
    val list2: List[Int] = List(1)
    list2 match {
      case first :: second :: rest => println(first + "-" + second + "-" + rest)
      case _ => println("something else")
    }

    println("========匹配元组=========")
    for (tuple <- Array((0, 1), (1, 0), (1, 1), (1, 0, 2),(1, 0, 3))) {
      val result = tuple match {
        case (0, _) => "0 ..." //是第一个元素是0的元组
        case (y, 0) => "" + y + "0" // 匹配后一个元素是0的对偶元组
        case (a, b) => "" + a + " " + b
        case _ => "something else" //默认
      }
      println(result)
    }

    val map: Map[String, Int] = Map(
      ("a", 21), ("b", 2), ("c", 3) ,("d", 2)
    )
    // ("b", 2) ("d", 2)符合匹配条件
    for ((x, 2) <- map) {
      println(x+2)
    }//b2 d2

    val list3 = List(
      (("河北", "鞋"),1), // ("河北",("鞋", 2))
      (("河南", "衣服"),2), // ("河南", ("衣服",4))
      (("华北", "电脑"),3) // ("华北", ("电脑",6))
    )
    val newlist = list3.map {
      //将符合格式的集合转化格式
      case ((prv, item), cnt) => {
        (prv, (item, cnt * 2))
      }
    }
    println(newlist)//List((河北,(鞋,2)), (河南,(衣服,4)), (华北,(电脑,6)))
  }
}
